home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / ir / field_search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  6.3 KB  |  217 lines

  1. /* 
  2.  * field.c -- 
  3.  * SCCS Status     : %W%    %G%
  4.  * Author          : Huynh Quoc T. Tung
  5.  * Created On      : Mon Feb 14 11:18:44 1994
  6.  * Last Modified By: Ulrich Pfeifer
  7.  * Last Modified On: Wed Apr  6 18:55:10 1994
  8.  * Update Count    : 42
  9.  * Status          : Unknown, Use with caution!
  10.  */
  11.  
  12. /*
  13. #include <stdio.h>
  14. #include <string.h>
  15. */
  16. #include "futil.h"
  17. #include "irfiles.h"
  18. #include "irtfiles.h"
  19. #include "cutil.h"
  20.  
  21. #define MAX_WORD_LENGTH 20
  22. #define MAX_LINE_LENGTH 1000
  23.  
  24. long number_of_operands = 1;
  25.  
  26. /* ------------------------------------------------------------- */
  27.  
  28. /* return true if it is field,
  29.    return false if the length of field less than 2 
  30.    and it is not field 
  31. */
  32.  
  33. boolean is_field_name(word)
  34.      char* word;
  35. {
  36.   if(word[strlen(word) - 1] == FIELD_EQUAL) { /* word is field */
  37.     word[strlen(word) - 1] = '\0';
  38.     if(strlen(word) < 2)
  39.       return(false);
  40.     else return(true);
  41.   }
  42.   else return(false);
  43. }
  44. /* ------------------------------------------------------------- */
  45.  
  46. /* return the field_id of the field_name,
  47.    else -1.
  48. */
  49.  
  50. long pick_up_field_id(field_name, db)
  51.      char* field_name;
  52.      database* db;
  53. {
  54.   long i;
  55.   
  56.   for(i=0; i < db->number_of_fields; i++) 
  57.     if(strcmp(field_name, db->fields[i].field_name) == 0)
  58.       return(i);
  59.   return(-1);
  60. }
  61. /* ------------------------------------------------------------- */
  62.  
  63. static boolean field_exist _AP((char* field_name, long number_of_elements,
  64.                                 fields_struct* fields));
  65. static boolean field_exist(field_name, number_of_elements, fields)
  66.      char* field_name;
  67.      long number_of_elements;
  68.      fields_struct* fields;
  69. {
  70.   while(number_of_elements > 0) {
  71.     if(fields[number_of_elements-1].field_name != NULL)
  72.       if(strcmp(field_name, 
  73.                 fields[number_of_elements-1].field_name))
  74.         number_of_elements--;
  75.       else return(true);
  76.     else number_of_elements--;
  77.   }
  78.   return(false);
  79. }
  80. /* ------------------------------------------------------------- */
  81.  
  82. static void clear_field_array _AP((char** field_array, long number_of_fields));
  83. static void clear_field_array(field_array, number_of_fields)
  84.      char** field_array;
  85.      long number_of_fields;
  86. {
  87.   long i;
  88.   
  89.   if(number_of_fields > 0) 
  90.     for(i=0; i < number_of_fields; i++)
  91.       s_free(field_array[i]);
  92. }
  93. /* ------------------------------------------------------------- */
  94.  
  95. /* 
  96.   insert the field_names which the query contains,
  97.   return number of fields.
  98.  */
  99.  
  100. long insert_fields(field_array, number_of_fields, db)
  101.      char** field_array;
  102.      long number_of_fields;
  103.      database* db;
  104. {
  105.   long len;
  106.   long i;
  107.   char* field_name;
  108.  
  109.   if(db->fields == NULL)
  110.     db->fields = (field_db*)s_malloc((size_t)(sizeof(field_db) * (number_of_fields + 1)));
  111.  
  112.   db->number_of_fields = number_of_fields;
  113.  
  114.   for(i=0; i< number_of_fields; i++) {
  115.     len = strlen(field_array[i]);
  116.     if((field_array[i][len-1] == EQ) || 
  117.        (field_array[i][len-1] == GREATER) ||
  118.        (field_array[i][len-1] == LESS)) {
  119.       field_array[i][len-1] = '\0';
  120.       db->fields[i].numeric = true;
  121.     }
  122.     else db->fields[i].numeric = false;
  123.     
  124.     field_name = string_downcase(field_array[i]);
  125.     if(db->fields[i].field_name == NULL)
  126.       db->fields[i].field_name =
  127.         (char*)s_malloc((size_t)(sizeof(char) * (MAX_WORD_LENGTH + 1)));
  128.     s_strncpy(db->fields[i].field_name, field_name, MAX_WORD_LENGTH);
  129.     db->fields[i].field_id = i;
  130.     db->fields[i].total_word_count = 0;
  131.   }
  132.   clear_field_array(field_array, number_of_fields);
  133.   return(db->number_of_fields);
  134. }
  135.  
  136. /* ------------------------------------------------------------- */
  137.  
  138. /* open only fields which the query contains */
  139.  
  140. boolean open_field_streams_for_search(field_name, global_dictionary_exists, db)
  141.      char* field_name;
  142.      int global_dictionary_exists;
  143.      database* db;
  144. {
  145.   long i;
  146.   boolean is_field_exist = false;
  147.   char file[1001];
  148.  
  149.   if(global_dictionary_exists) {
  150.     db->dictionary_stream = s_fopen(dictionary_filename(file, db), "rb");
  151.     if (db->dictionary_stream == NULL) { 
  152.       strcpy(field_name, "global database");
  153.       waislog(WLOG_HIGH,WLOG_ERROR,"can't open the word hash file %s\n",file); 
  154.       return(false);
  155.     }
  156.     if(false == look_up_total_word_count(db)) { /* side effects db */
  157.       strcpy(field_name, "global database");
  158.       return(false);
  159.     }
  160.     db->index_stream = s_fopen(index_filename(file, db), "rb");
  161.     if (db->index_stream == NULL) {
  162.       strcpy(field_name, "global database");
  163.       waislog(WLOG_HIGH, WLOG_ERROR,
  164.               "2can't open the inverted index file %s\n", 
  165.               file);
  166.       return(false);
  167.     }
  168.   }
  169.   
  170.   if(db->field_dictionary_streams == NULL)
  171.     db->field_dictionary_streams = 
  172.       (FILE**)s_malloc((size_t)(sizeof(FILE*) * (db->number_of_fields + 1)));
  173.   if(db->field_index_streams == NULL)
  174.     db->field_index_streams = 
  175.       (FILE**)s_malloc((size_t)(sizeof(FILE*) * (db->number_of_fields + 1)));
  176.  
  177.   for(i = 0; i < db->number_of_fields; i++) {
  178.     if(strncmp(db->fields[i].field_name, FREE_TEXT_FIELD, 
  179.                strlen(FREE_TEXT_FIELD))) {
  180.       db->field_dictionary_streams[i] = 
  181.         s_fopen(field_dictionary_filename(file, db->fields[i].field_name, db), 
  182.                 "rb");
  183.       if (db->field_dictionary_streams[i] == NULL) {
  184.         strcpy(field_name, db->fields[i].field_name);
  185.         waislog(WLOG_HIGH,WLOG_ERROR,"can't open the word hash file %s\n",file);
  186.         return(false);
  187.       }
  188.       else {
  189.         if(false == field_look_up_total_word_count(db->fields[i].field_name, i,
  190.                                                    db)) { /* side effects db */
  191.           strcpy(field_name, db->fields[i].field_name);
  192.           return(false);
  193.         }
  194.         is_field_exist = true;
  195.         db->fields[i].index_file_number = 0;
  196.       }
  197.       if (db->field_dictionary_streams[i] != NULL){ 
  198.         db->field_index_streams[i] = 
  199.           s_fopen(field_index_filename(file, db->fields[i].field_name, db), 
  200.                   "rb");
  201.         if(db->field_index_streams[i] == NULL) {
  202.           strcpy(field_name, db->fields[i].field_name);
  203.           waislog(WLOG_HIGH, WLOG_ERROR,
  204.                   "2can't open the inverted index file of field %s\n", 
  205.                   db->fields[i].field_name);
  206.           return(false);
  207.         }
  208.       }
  209.     }
  210.     else is_field_exist = true;
  211.   }
  212.   if(is_field_exist)
  213.     return(true);
  214.   else return(false);
  215. }
  216. /* ------------------------------------------------------------- */
  217.